package com.xinqi.modules.exam.student.controller.web;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.common.security.utils.UserHelp;
import com.xinqi.modules.exam.student.dto.req.CourseExamPaperErrorSetCreateDTO;
import com.xinqi.modules.exam.student.dto.req.CourseExamPaperErrorSetQueryDTO;
import com.xinqi.modules.exam.student.dto.req.CourseExamPaperErrorSetUpdateDTO;
import com.xinqi.modules.exam.student.dto.rsp.CourseExamPaperErrorSetResultDTO;
import com.xinqi.modules.exam.student.dto.rsp.CourseExamPaperErrorSetStatsDTO;
import com.xinqi.modules.exam.student.service.CourseExamPaperErrorSetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 课程测评学生错题集WEB控制层
 *
 * @author: linkai
 * @date: 2023/07/02
 */
@Tag(name = "Web-课程测评学生错题集", description = "课程测评学生错题集Web接口")
@RestController
@RequestMapping("/web/exam-paper")
@RequiredArgsConstructor
public class CourseExamPaperErrorSetWebController {
    private final CourseExamPaperErrorSetService courseExamPaperErrorSetService;

    /**
     * 课程测评学生错题集-分页
     */
    @Operation(summary = "课程测评学生错题集-分页")
    @GetMapping("/{exam-id}/error-set/page")
    public R<PageInfo<CourseExamPaperErrorSetResultDTO>> page(
        @Parameter(description = "测评ID", example = "100001") @PathVariable("exam-id") Long examId,
        @ParameterObject CourseExamPaperErrorSetQueryDTO dto
    ) {
        dto.setStudentId(UserHelp.userId());
        PageInfo<CourseExamPaperErrorSetResultDTO> page = courseExamPaperErrorSetService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 课程测评学生错题集-详情
     */
    @Operation(summary = "课程测评学生错题集-详情")
    @GetMapping("/{exam-id}/error-set/{id}")
    public R<CourseExamPaperErrorSetResultDTO> findById(
        @Parameter(description = "测评ID", example = "100001") @PathVariable("exam-id") Long examId,
        @Parameter(description = "id", example = "100001") @PathVariable("id") Long id
    ) {
        CourseExamPaperErrorSetResultDTO dto = courseExamPaperErrorSetService.findById(id);
        return R.data(dto);
    }

    /**
     * 错题集统计数据
     */
    @Operation(summary = "错题集统计数据")
    @GetMapping("/{exam-id}/error-set/stats-data")
    public R<CourseExamPaperErrorSetStatsDTO> statsData(
        @Parameter(description = "测评ID", example = "100001") @PathVariable("exam-id") Long examId,
        @ParameterObject CourseExamPaperErrorSetQueryDTO dto
    ) {
        CourseExamPaperErrorSetStatsDTO result = courseExamPaperErrorSetService.statsData(dto);
        return R.data(result);
    }

    /**
     * 新增课程测评学生错题集
     */
    @Operation(summary = "新增课程测评学生错题集")
    @PostMapping("/{exam-id}/error-set")
    public R<Long> insert(
        @Parameter(description = "测评ID", example = "100001") @PathVariable("exam-id") Long examId,
        @RequestBody CourseExamPaperErrorSetCreateDTO dto
    ) {
        return R.data(courseExamPaperErrorSetService.insert(dto));
    }

    /**
     * 修改课程测评学生错题集
     */
    @Operation(summary = "修改课程测评学生错题集")
    @PutMapping("/{exam-id}/error-set/{id}")
    public R<Boolean> update(
        @Parameter(description = "测评ID", example = "100001") @PathVariable("exam-id") Long examId,
        @Parameter(description = "id", example = "100001") @PathVariable("id") Long id,
        @RequestBody CourseExamPaperErrorSetUpdateDTO dto
    ) {
        dto.setId(id);
//        dto.setExamPaperId(examId);
        return R.data(courseExamPaperErrorSetService.updateById(dto));
    }

    /**
     * 删除课程测评学生错题集
     */
    @Operation(summary = "删除课程测评学生错题集")
    @DeleteMapping("/{exam-id}/error-set/{id}")
    public R<Boolean> delete(
        @Parameter(description = "测评ID", example = "100001") @PathVariable("exam-id") Long examId,
        @Parameter(description = "id", example = "100001") @PathVariable("id") Long id
    ) {
        return R.data(courseExamPaperErrorSetService.deleteById(id));
    }
}
